package com.itheima.web.controller.store;

import com.github.pagehelper.PageInfo;
import com.itheima.domain.store.Member;
import com.itheima.domain.store.Orders;
import com.itheima.domain.store.Product;
import com.itheima.service.store.OrdersService;
import com.itheima.web.controller.BaseServlet;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/order/list")
public class OrdersServlet extends BaseServlet {
    public void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //进入数据获取列表

        int page = 1;
        int size = 5;
        if (StringUtils.isNotBlank(request.getParameter("page"))) {
            page = Integer.parseInt(request.getParameter("page"));
        }
        if (StringUtils.isNotBlank(request.getParameter("size"))) {
            size = Integer.parseInt(request.getParameter("size"));
        }
        PageInfo all = ordersService.findAll(page, size);
        //将数据保存到指定的位置
        request.setAttribute("page", all);

        //跳转页面
        request.getRequestDispatcher("/WEB-INF/pages/store/order/orders-list.jsp").forward(request, response);


    }

    public void toEdit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //查询要修改的数据findById
        String id = request.getParameter("id");
        Orders orders = ordersService.findById(id);
   /*     Member member = orders.getMember();
        System.out.println(member);*/


        //将数据加载到指定区域，供页面获取
        request.setAttribute("orders",orders);
        System.out.println(orders);

        //跳转页面
        request.getRequestDispatcher("/WEB-INF/pages/store/order/orders-show.jsp").forward(request,response);
    }


    public void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String[] ids = request.getParameterValues("ids");
        ordersService.delete(ids);
        ordersService.deletePo(ids);
        response.sendRedirect(request.getContextPath()+"/order/list?operation=list");

    }
    //      protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        this.doPost(request, response);
//    }

}
